import router from '@/router'
import VueRouter from 'vue-router'
import defaultRoutes from '@/router/routes/defaultRoutes'
import asyRoutes from '@/router/routes/asyRoutes'

/**
 *  路由方法vueRouter
 */

// 动态添加路由方法
export const addAsyncRoute = (prouteName, asyncRoutes) => {
  asyncRoutes.forEach(asyncRoute => {
    const route = asyRoutes.find(asyRoute => {
      return asyncRoute.route_name === asyRoute.name
    })
    if (route) {
      route.meta.title = asyncRoute.title || ''
      prouteName ? router.addRoute(prouteName, route) : router.addRoute(route)
    }
    if (asyncRoute.children) {
      addAsyncRoute(asyncRoute.route_name, asyncRoute.children)
    }
  })
}
// 重置路由 清空动态路由方法
export const resetRouter = () => {
  const newRouter = new VueRouter({
    routes: defaultRoutes
  })
  router.matcher = newRouter.matcher
  // defaultRoutes.forEach(route => {
  //   router.addRoute(route)
  // })
}
